<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use DB;


class AdminMenu extends Authenticatable
{
    use Notifiable;
	protected $table = 'admin_menu';


    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
         'remember_token',
    ];

	public static function display($menuName, $type = null, array $options = [])
	{
		if(!$type){
			$items = AdminMenu::all()->toArray();
		}else{
			$items = AdminMenu::where('parent_id',$type)->get()->toArray();
		}
		return $items;
	}

	public static function getAllMenuTitle(){
		$menus = DB::table('admin_menu')
			->select('id','parent_id','title','uri')
			->get()
			->toArray();
		if(!$menus){
			return array();
		}
		return $menus;
	}

	public static function getMenuChildren($pid){
		$menus = DB::table('admin_menu')
			->where('parent_id',$pid)
			->select('id','parent_id','title','uri')
			->get()
			->toArray();
		if(!$menus){
			return array();
		}
		return $menus;
	}



	/*
	 * 登录成功
	 * 根据用户id查出角色
	 * 根据角色查出可见的菜单的id
	 * 循环遍历菜单列表，判断菜单id = 可见菜单id,则显示，否则不显示
	 *  SELECT a.id,b.role_id,c.menu_id FROM c_users AS a
		LEFT JOIN c_admin_role_users AS b ON a.id=b.user_id
		LEFT JOIN c_admin_role_menu AS c ON b.role_id=c.role_id
		WHERE a.id=1

	 * */
	public static function getUsersMenuPermission($id){
		$menuIdArr = array();
		$menuIdObj = DB::table('users')
			->leftjoin('admin_role_users', 'users.id', '=', 'admin_role_users.user_id')
			->leftjoin('admin_role_menu', 'admin_role_users.role_id', '=', 'admin_role_menu.role_id')
			->where('users.id',$id)
			->select('admin_role_menu.menu_id')
			->get()
			->toArray();
		if(!$menuIdObj){
			return array();
		}
		foreach ($menuIdObj as $menuId){
			$menuIdArr[] = $menuId->menu_id;
		}

		return $menuIdArr;

	}

	public static function getMenuIdToUrl($url){
		$id = '';
		$idArr = DB::table('admin_menu')->where('uri',$url)->pluck('id');
		if($idArr){
			$id=$idArr[0];
		}
		return $id;
	}


	public static function getMenuOptionName(){
		$menuP = DB::table('admin_menu_option')
			->leftjoin('admin_option', 'admin_menu_option.option_id', '=', 'admin_option.id')
			->select('admin_menu_option.menu_id','admin_menu_option.option_id','admin_option.option_name')
			->get()
			->toArray();
		if(!$menuP){
			return array();
		}
		return $menuP;
	}


}
